package com.hex.liyue.usermodule.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.Selection;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.fragment.BaseFragment;
import com.hex.liyue.mainmodule.activity.MainActivity;
import com.hex.liyue.usermodule.presenter.ILoginView;
import com.hex.liyue.usermodule.presenter.LoginPresenter;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 这是一个登录界面
 * Created by yukoyuan on 16/6/13.
 */
public class LoginFragment extends BaseFragment implements ILoginView {

    @Bind(R.id.et_login_phone)
    EditText etLoginPhone;
    @Bind(R.id.et_login_pwd)
    EditText etLoginPwd;
    @Bind(R.id.iv_password)
    ImageView ivPassword;

    private LoginPresenter loginPresenter;
    private RegistFragment registFragment;
    private ForgetPwdFragment forgetPwdFragment;
    private VCodeLoginFragment vCodeLoginFragment;
    private boolean flagNow = true;

    @Override
    public void initdata(Bundle arguments) {

    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_login;
    }

    @Override
    protected void bindPresenter() {
        loginPresenter = new LoginPresenter(this);
    }

    @Override
    protected void initToolbar() {

    }


    @Override
    public void showLod(String msg) {
        showLoad(getActivity(), msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(getActivity(), msg);
    }

    @Override
    public String getPhone() {
        return etLoginPhone.getText().toString().trim();
    }

    @Override
    public String getPwd() {
        return etLoginPwd.getText().toString().trim();
    }

    @Override
    public void startRegistFragment() {
        if (registFragment == null) {
            registFragment = new RegistFragment();
        }
        //跳转注册页面
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.left_out);
        fragmentTransaction.addToBackStack("registFragment");
        fragmentTransaction.replace(R.id.fl_login_regist, registFragment).commit();
    }

    @Override
    public void startVCodeLoginFragment() {
        if (vCodeLoginFragment == null) {
            vCodeLoginFragment = new VCodeLoginFragment();
        }
        //跳转验证码登录的页面
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.left_out);
        fragmentTransaction.addToBackStack("vCodeLoginFragment");
        fragmentTransaction.replace(R.id.fl_login_regist, vCodeLoginFragment).commit();
    }

    @Override
    public void startForgetPwdFragment() {
        if (forgetPwdFragment == null) {
            forgetPwdFragment = new ForgetPwdFragment();
        }
        //跳转忘记密码的页面
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.left_out);
        fragmentTransaction.addToBackStack("forgetPwdFragment");
        fragmentTransaction.replace(R.id.fl_login_regist, forgetPwdFragment).commit();
    }

    @Override
    public void startMainActivity() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
        getActivity().finish();
    }


    @OnClick({R.id.tv_login_regist, R.id.tv_login_forget_pwd, R.id.bt_login, R.id.tv_login_vcode_login,R.id.iv_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login_regist:
                startRegistFragment();
                break;
            case R.id.tv_login_forget_pwd:
                startForgetPwdFragment();
                break;
            case R.id.bt_login:
                loginPresenter.login();
                break;
            case R.id.tv_login_vcode_login:
                startVCodeLoginFragment();
                break;
            case R.id.iv_password:
                if (flagNow) {
                    etLoginPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    flagNow = false;
                    ivPassword.setBackgroundResource(R.drawable.show_password_write);
                    Selection.setSelection(etLoginPwd.getText(),etLoginPwd.getText().length());
                } else {
                    //否则隐藏密码
                    etLoginPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    flagNow =true;
                    ivPassword.setBackgroundResource(R.drawable.no_show_password_write);
                    Selection.setSelection(etLoginPwd.getText(),etLoginPwd.getText().length());
                }
                break;
        }
    }
}
